package GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import MultiThread.MultiThread;
import SingleThread.SingleThread;


public class monitor extends JFrame implements ActionListener{
	
	private static final long serialVersionUID = 1L;
	public JComboBox callPutFlag = new JComboBox();
	public JComboBox s = new JComboBox();		
	public JComboBox x = new JComboBox();
	public JComboBox t = new JComboBox();
	public JComboBox r = new JComboBox();
	public JComboBox b =new JComboBox();
	public JComboBox v = new JComboBox();
	public JComboBox nSteps = new JComboBox();
	public JComboBox nSimulations = new JComboBox();
	
	public JLabel callPutFlaglab = new JLabel("Call or Put:");
	public JLabel slab = new JLabel("s:");		
	public JLabel xlab = new JLabel("x:");
	public JLabel tlab = new JLabel("t:");
	public JLabel rlab = new JLabel("r:");
	public JLabel blab =new JLabel("b:");
	public JLabel vlab = new JLabel("v:");
	public JLabel nStepslab = new JLabel("Steps:");
	public JLabel nSimulationslab = new JLabel("Simulation:");
	
	public JProgressBar pb = new JProgressBar();
	public JLabel result  = new JLabel("Select a method");
	private JButton startSingleThreadButton = new JButton("Single Thread");
	private JButton startMultiThreadButton = new JButton("Multi Thread");
	private JButton quitButton = new JButton("Exit");
	
	public monitor(ArrayList<String[]> s){
		super();
		build(s);
	}
	
	private void build(ArrayList<String[]> s){
		setTitle("Monte Carlo Simulations");
		setSize(1000,400);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//layout initialization
		JPanel pane = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		setValues(s);
	
		c.gridx = 0;
		c.gridy = 0;
		pane.add(callPutFlaglab, c);
		
		c.gridx = 1;
		c.gridy = 0;
		pane.add(callPutFlag, c);
		
		
		c.gridx = 2;
		c.gridy = 0;
		pane.add(this.slab, c);
		
		c.gridx = 3;
		c.gridy = 0;
		pane.add(this.s, c);

		
		c.gridx = 4;
		c.gridy = 0;
		pane.add(xlab, c);
		
		
		c.gridx = 5;
		c.gridy = 0;
		pane.add(x, c);
		
		c.gridx = 6;
		c.gridy = 0;
		pane.add(tlab, c);
		
		
		c.gridx = 7;
		c.gridy = 0;
		pane.add(t, c);
		
		c.gridx = 6;
		c.gridy = 1;
		pane.add(rlab, c);
		
		c.gridx = 7;
		c.gridy = 1;
		pane.add(r, c);
		
		
		c.gridx = 0;
		c.gridy = 1;
		pane.add(blab, c);
		
		c.gridx = 1;
		c.gridy = 1;
		pane.add(b, c);

		
		c.gridx = 2;
		c.gridy = 1;
		pane.add(vlab, c);
	
		c.gridx = 3;
		c.gridy = 1;
		pane.add(v, c);
	
		c.gridx = 4;
		c.gridy = 1;
		pane.add(nStepslab, c);
		
		c.gridx = 5;
		c.gridy = 1;
		pane.add(nSteps, c);
		
		pb.setStringPainted(true);
		c.gridx = 5;
		c.gridy = 3;
		pane.add(pb, c);
		
		startSingleThreadButton.addActionListener(this);
		c.gridx = 1;
		c.gridy = 3;
		pane.add(startSingleThreadButton, c);
		
		startMultiThreadButton.addActionListener(this);
		c.gridx = 3;
		c.gridy = 3;
		pane.add(startMultiThreadButton, c);
		
		c.gridx = 3;
		c.gridy = 5;
		pane.add(result,c);
		
		
		quitButton.addActionListener(this);
		c.gridx = 3;
		c.gridy = 6;
		pane.add(quitButton, c);
		
		this.setContentPane(pane);
	}
	
	private void setValues(ArrayList<String[]> s){
		
		for(int i=0; i<s.size(); i++){
				String[] set = s.get(i);
				callPutFlag.addItem(set[0]);
				this.s.addItem(set[1]);
				x.addItem(set[2]);
				t.addItem(set[3]);
				r.addItem(set[4]);
				b.addItem(set[5]);
				v.addItem(set[6]);
				nSteps.addItem(set[7]);
		}
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if(event.getSource().equals(this.startSingleThreadButton)){
			new Thread(new SingleThread(this)).start();
		}
		
		else if(event.getSource().equals(this.startMultiThreadButton)){
			try {
				new MultiThread(this).call();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(event.getSource().equals(this.quitButton)){
			System.exit(0);
		}
		
	}

}
